package com.live.dengdengtv.fragment.hall;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.live.dengdengtv.R;
import com.live.dengdengtv.activity.SearchActivity;
import com.live.dengdengtv.activity.login.PersonalInfoActivity;
import com.live.dengdengtv.activity.my.AuthActivity;
import com.live.dengdengtv.activity.msg.FansListActivity;
import com.live.dengdengtv.activity.msg.FollowActivity;
import com.live.dengdengtv.activity.msg.FriendListActivity;
import com.live.dengdengtv.activity.msg.MyMsgActivity;
import com.live.dengdengtv.activity.my.ContributionActivity;
import com.live.dengdengtv.activity.my.MyAccountActivity;
import com.live.dengdengtv.activity.my.MyLevelActivity;
import com.live.dengdengtv.activity.my.MyLiveActivity;
import com.live.dengdengtv.activity.my.MyProfitsActivity;
import com.live.dengdengtv.activity.setting.SettingActivity;
import com.live.dengdengtv.bean.AppInfo;
import com.live.dengdengtv.bean.LoginInfo;
import com.live.dengdengtv.fragment.BaseFragment;
import com.live.dengdengtv.kernel.utils.SxbLog;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.io.File;

/**
 * Created by wyf on 2016/8/1.
 */
public class MyInfoFragent extends BaseFragment implements View.OnClickListener{

    private static MyInfoFragent myInfoFragent = new MyInfoFragent();
    private static String TAG = "MyInfoFragent";
    private TextView edit_id_tv;
    private TextView edit_nickName_tv;
    private TextView edit_sign_tv;
    private ImageView avatar_iv;

    private View contentView;

    public static MyInfoFragent getInstance(){
        return myInfoFragent;
    }
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        if(contentView==null) {

            contentView = inflater.inflate(R.layout.frag_myinfo_white, container, false);

            avatar_iv = (ImageView) contentView.findViewById(R.id.avatar_iv);
            avatar_iv.setOnClickListener(this);

            contentView.findViewById(R.id.editInfo_ll).setOnClickListener(this);

            edit_id_tv = (TextView) contentView.findViewById(R.id.textView7);
            edit_nickName_tv = (TextView) contentView.findViewById(R.id.textView8);
            edit_sign_tv = (TextView) contentView.findViewById(R.id.textView9);


            contentView.findViewById(R.id.follow_ll).setOnClickListener(this);
            contentView.findViewById(R.id.fans_ll).setOnClickListener(this);
            contentView.findViewById(R.id.myProfit_ll).setOnClickListener(this);

            contentView.findViewById(R.id.videoAction_rl).setOnClickListener(this);
            contentView.findViewById(R.id.classAction_rl).setOnClickListener(this);
            contentView.findViewById(R.id.accountAction_rl).setOnClickListener(this);
            contentView.findViewById(R.id.contriAction_rl).setOnClickListener(this);
            contentView.findViewById(R.id.authAction_rl).setOnClickListener(this);
            contentView.findViewById(R.id.setting_rl).setOnClickListener(this);

            contentView.findViewById(R.id.title_left_iv).setOnClickListener(this);
            contentView.findViewById(R.id.msg_iv).setOnClickListener(this);
            contentView.findViewById(R.id.follow_iv).setOnClickListener(this);

            setData(contentView);
        }
        return contentView;
    }

    private void setData(View view) {
        edit_id_tv.setText("ID："+AppInfo.loginInfo.getUserId());

        if(!TextUtils.isEmpty(AppInfo.loginInfo.getUserNickname())){
            edit_nickName_tv.setText(AppInfo.loginInfo.getUserNickname());
        }
        if(!TextUtils.isEmpty(AppInfo.loginInfo.getUserSign())){
            edit_sign_tv.setText("个性签名："+AppInfo.loginInfo.getUserSign());
        }
        String avatarAddress = AppInfo.loginInfo.getAvatarAddress();
        if(!TextUtils.isEmpty(avatarAddress)){
            if(avatarAddress.startsWith("http")){
                ImageLoader.getInstance().displayImage(avatarAddress, avatar_iv);
            }else {
                avatar_iv.setImageURI(Uri.fromFile(new File(avatarAddress)));
            }
        }


        TextView myProfit_tv = (TextView) contentView.findViewById(R.id.myProfit_tv);
        myProfit_tv.setText(LoginInfo.getInstance().getVirTicket()+"");

    }


    private void goFollowActivity() {
        Intent intent = new Intent(getActivity(), FriendListActivity.class);
        startActivity(intent);
    }
    private void goMsgActivity() {
        Intent intent = new Intent(getActivity(), MyMsgActivity.class);
        startActivity(intent);
    }
    private void goSearch() {
        Intent intent = new Intent(getActivity(), SearchActivity.class);
        startActivity(intent);
    }
    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        switch (v.getId()){

            case R.id.title_left_iv :
                goSearch(); return;
            case R.id.follow_iv :
                goFollowActivity(); return;
            case R.id.msg_iv :
                goMsgActivity(); return;

            case R.id.avatar_iv:
            case R.id.editInfo_ll:
                intent.putExtra("display","red");
                intent.setClass(getActivity(), PersonalInfoActivity.class);
                break;
            case R.id.follow_ll:
                intent.setClass(getActivity(), FollowActivity.class);
                break;
            case R.id.fans_ll:
                intent.setClass(getActivity(), FansListActivity.class);
                break;
            case R.id.myProfit_ll:
                intent.setClass(getActivity(), MyProfitsActivity.class);
                break;
            case R.id.videoAction_rl:
                intent.setClass(getActivity(), MyLiveActivity.class);
                break;
            case R.id.classAction_rl:
                intent.setClass(getActivity(), MyLevelActivity.class);
                break;
            case R.id.accountAction_rl:
                intent.setClass(getActivity(), MyAccountActivity.class);
                break;
            case R.id.contriAction_rl:
                intent.setClass(getActivity(), ContributionActivity.class);
                break;
            case R.id.authAction_rl:
                intent.setClass(getActivity(), AuthActivity.class);
                break;
            case R.id.setting_rl:
                intent.setClass(getActivity(), SettingActivity.class);
                break;
            default: return;
        }

        startActivityForResult(intent, 100);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != Activity.RESULT_OK){
            SxbLog.e(TAG, "onActivityResult->failed for request: " + requestCode + "/" + resultCode);
            return;
        }

        switch (requestCode) {
            case 100:
                String avatarAddress = AppInfo.loginInfo.getAvatarAddress();
                if(!TextUtils.isEmpty(avatarAddress)){
                    //TODO:  需要改
//                    Bitmap bitmap = BitmapFactory.decodeFile(AppInfo.loginInfo.getAvatarAddress());
//                    avatar_iv.setImageBitmap(bitmap);

                    if(avatarAddress.startsWith("http")){
                        ImageLoader.getInstance().displayImage(avatarAddress, avatar_iv);
                    }else {
                        avatar_iv.setImageURI(Uri.fromFile(new File(avatarAddress)));
                    }
                }

                if(!TextUtils.isEmpty(AppInfo.loginInfo.getUserNickname())){
                    edit_nickName_tv.setText(AppInfo.loginInfo.getUserNickname());
                }

                if(!TextUtils.isEmpty(AppInfo.loginInfo.getUserSign())){
                    edit_sign_tv.setText("个性签名："+AppInfo.loginInfo.getUserSign());
                }

                break;

        }

    }

    //    @Override
//    public void onViewCreated(View view, Bundle savedInstanceState) {
//        super.onViewCreated(view, savedInstanceState);
//
//        Bundle args = getArguments();
//
//        if (args != null) {
//            TextView title = (TextView) view;
//            title.setText("Title: " + args.getCharSequence(KEY_TITLE));
//
//        }
//    }
}
